﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using CARMSData.CollectionClasses;
using CARMSData.HelperClasses;
using CARMSData.StoredProcedureCallerClasses;

namespace CARMS
{

    public class ConditionClass
    {
        public static void BindPublicItemByCategoryID(ListControl objControl, string strCategoryID)
        {
            objControl.Items.Clear();
            if (strCategoryID.Trim().Length > 0)
            {
                var collection = new DictionaryCollection();
                collection.GetMulti(DictionaryFields.CategoryId == new Guid(strCategoryID));
                objControl.DataSource = collection;
                objControl.DataTextField = "Name";
                objControl.DataValueField = "OID";
                objControl.DataBind();

                objControl.Items.Insert(0, new ListItem
                                               {
                                                   Text = "--ALL--",
                                                   Value = ""
                                               });
            }
        }


        public static void BindTableList(ListControl objControl)
        {

            objControl.Items.Clear();
            var collection =RetrievalProcedures.GetTableList();
            objControl.DataSource = collection;
            objControl.DataTextField = "Name";
            objControl.DataValueField = "Name";
            objControl.DataBind();

        }

        public static void BindActionFolder(ListControl objControl)
        {

            objControl.Items.Clear();

            var collection = new ActionCollection();

            collection.GetMulti(null);

            AddDropdownlist(objControl, string.Empty, collection, "└─");


        }

        public static void AddDropdownlist(ListControl objControl, string parentID, ActionCollection tableInput, string strPre)
        {
            var dvTree = from item in tableInput orderby item.ActionName where item.ParentId == parentID select item;

            foreach (var row in dvTree)
            {
                var currentPre = string.Empty;
                if (parentID == string.Empty)
                {
                    var item = new ListItem(strPre + row.ActionName, row.ActionId);
                    objControl.Items.Add(item);
                    AddDropdownlist(objControl, row.ActionId, tableInput, strPre);
                }
                else
                {
                    currentPre = "　　" + strPre;
                    objControl.Items.Add(new ListItem(currentPre + row.ActionName, row.ActionId));
                    AddDropdownlist(objControl, row.ActionId, tableInput, strPre);
                }

            }
        }
    }

}
